﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.Validation;
using StyleMVVM.WCF.LanguageExtensions;

namespace StyleMVVM.WCF.Validation
{
	[ExportPriority(10)]
	[Export(typeof(IValidationContextProvider))]
	public class WCFValidationContextProvider : IValidationContextProvider
	{
		public IValidationContext ProvideContext(object validationObject,
		                                         bool monitor,
		                                         bool shared,
		                                         CreateNewContextDelegate createDelegate)
		{
			IValidationContext returnValue = null;

			if (shared)
			{
				IDictionary<object, object> items = OperationContext.Current.Items();
				returnValue = items[validationObject] as IValidationContext;

				if (returnValue == null)
				{
					returnValue = createDelegate(validationObject, monitor, shared);

					items[validationObject] = returnValue;
				}
			}
			else
			{
				returnValue = createDelegate(validationObject, monitor, false);
			}

			return returnValue;
		}
	}
}